﻿using System;
using Animoso.Domain;
using Animoso.Domain.Data;
using NHibernate;
using NHibernate.Criterion;

namespace Animoso.Data
{
    public class UserRepository : BaseRepository<User>, IUserRepository
    {
        public bool ValidateUser(string username, string password)
        {
            using (ISession session = SessionProvider.GetSession())
            {
                User user = session.CreateCriteria(typeof(User))
                   .Add(Expression.Eq("Name", username))
                   .Add(Expression.Eq("Password", password))
                   .UniqueResult<User>();

                if (user == null)
                    return false;
                return true;
            }
        }

        public User GetByUsername(string username)
        {
            using (ISession session = SessionProvider.GetSession())
            {
                User user = session.CreateCriteria(typeof (User))
                    .Add(Expression.Eq("Name", username))
                    .UniqueResult<User>();

                if(user == null)
                    throw new NotFoundException("User does not exist");
                return user;
            }
        }
    }
}
